package com.example.innovate.controller;

/*
    2021.9.13 update/
 */

import com.example.innovate.bean.Result;
import com.example.innovate.domain.UserInfoDO;
import com.example.innovate.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/api/register")
@Api(tags = "用户注册接口")
public class RegisterController {
    @Autowired
    UserService userService;


    @ResponseBody
    @PostMapping("/userRegister")
    @ApiOperation("用户注册")
    public Result userRegister(@RequestBody UserInfoDO userInfoDO) throws Exception{
        String username = userInfoDO.getUserInfoUserName();
        String password = userInfoDO.getUserInfoPassword();
        //        校验账号是否存在
        UserInfoDO userInfoDO1 = userService.getUserByName(username.trim());
        //        账号存在
        if(userInfoDO1 != null){
            return new Result(400,"error","此用户名已被使用","null");
        }
        //        账号去掉前后空格后，还包含空格
        if(username.trim().contains(" ")){
            return new Result(400,"error","格式错误，不能包含空格","null");
        }
        //        密码包含空格
        if(password.contains(" ")){
            return new Result(400,"error","格式错误，不能包含空格","null");
        }

        userInfoDO.setUserInfoIsAdmin(false);
        userInfoDO.setUserInfoIsSuperAdmin(false);
        userInfoDO.setUserInfoIsFrozen(false);
        userInfoDO.setUserInfoToken(null);
        userService.addUser(userInfoDO);
        userInfoDO.setUserInfoPassword(null);
        return new Result(200,"success","创建成功",userInfoDO);
    }
}
